package com.yjzx.util.racket.config;

import com.aliyun.openservices.ons.api.ONSFactory;
import com.aliyun.openservices.ons.api.Producer;
import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.transaction.TransactionProducer;
import com.aliyun.openservices.ons.api.transaction.TransactionStatus;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @author yjzx
 * @date 2024/8/17
 * @description
 */
@Configuration
@EnableConfigurationProperties({RocketProperties.class})
public class RocketConfig {
    @Bean(value = "producer",initMethod = "start",destroyMethod = "shutdown")
    public Producer producer(RocketProperties rocketProperties) {
        Properties mqProperties = rocketProperties.getMqProperties();
        return ONSFactory.createProducer(mqProperties);
    }
    @Bean(value = "transactionProducer",initMethod = "start",destroyMethod = "shutdown")
    public TransactionProducer transactionProducer(RocketProperties rocketProperties) {


        Properties mqProperties = rocketProperties.getMqProperties();
        TransactionProducer producer = ONSFactory.createTransactionProducer(
                mqProperties, (message) -> {
                    // 这里可以根据消息内容查询数据库来判断事务状态
                    return TransactionStatus.CommitTransaction;
                });
        return producer;
    }

/*    @DependsOn({"encryptMeterMessageListener"})
    @Bean(name = "encryptMeterConsumer", initMethod = "start", destroyMethod = "shutdown")
    public Consumer encryptMeterConsumer(@Qualifier("encryptMeterMessageListener") MessageListener messageListener, RocketProperties rocketProperties) {
        String exchangeName = "encryptMeter";
        RocketProperties.ProductProperties properties = rocketProperties.getProductProperties(exchangeName);

        Consumer consumer = ONSFactory.createConsumer( getProperties(rocketProperties,"encryptMeter",PropertyValueConst.BROADCASTING,1));
        consumer.subscribe(properties.getTopic(),
                properties.getTag(),
                messageListener);
        return consumer;
    }*/

    /**
     * 获取配置
     * @param rocketProperties rocket配置
     * @param exchangeName 队列名
     * @param messageModel 消息模式  BROADCASTING广播模式  CLUSTERING 聚合模式（消息只会被其中一个消费）
     * @param ThreadNums 线程数量
     * @return
     */
    private Properties getProperties(RocketProperties rocketProperties,String exchangeName,String messageModel,int ThreadNums) {
        Properties properties = rocketProperties.getMqProperties();
        RocketProperties.ProductProperties productProperties = rocketProperties.getProductProperties(exchangeName);
        //在控制台创建的 Group ID
        properties.put(PropertyKeyConst.GROUP_ID, productProperties.getGroupId());
        //消息集群模式
        properties.put(PropertyKeyConst.MessageModel, messageModel);
        //单线程
        properties.put(PropertyKeyConst.ConsumeThreadNums, ThreadNums);
        return properties;
    }

}
